package com.water.dmdb.util;

import com.water.dmdb.db.DbModel;

import java.sql.*;
import java.util.*;

public class DbUtil {

    /**
     * @param sql
     * @param dbType 数据库类型枚举
     * @return
     * @throws SQLException
     */
    public static List<Map<String, Object>> execute(String sql, DbType dbType) throws SQLException {
        List<Map<String, Object>> list = null;
        DbModel resultSet = getResultSet(sql, dbType);
        if (resultSet.getResultSet() != null && resultSet.getStatement() != null) {
            list = getMapping(resultSet);
        }
        return list;
    }

    /**
     * @param sql 执行sql
     * @return 返回原始执行sql结果
     * @throws SQLException
     */
    private static DbModel getResultSet(String sql, DbType dbType) throws SQLException {
        DbModel dbModel;
        Connection conn = null;
        try {
            if (dbType == DbType.DM) {
                conn = JdbcUtil.getDmConnection();
            } else if (dbType == DbType.ORACLE) {
                conn = JdbcUtil.getOracleConnection();
            } else if (dbType == DbType.MYSQL) {
                conn = JdbcUtil.getMYSQLConnection();
            }
            Statement statement = conn.createStatement();
            dbModel = new DbModel();
            String finalSql = sql.toLowerCase(Locale.ROOT);
            if (finalSql.contains("select") && !finalSql.contains("create") && !finalSql.contains("insert") && !finalSql.contains("delete") && !finalSql.contains("update") && !finalSql.contains("alter") && !finalSql.contains("drop")) {
                dbModel.setResultSet(statement.executeQuery(finalSql));
                dbModel.setStatement(statement);
            } else {
                statement.executeUpdate(finalSql);
            }
        } catch (SQLException e) {
            throw e;
        } finally {
            if (conn != null) {
                JdbcUtil.CloseConnection(conn, null, null);
            }
        }
        return dbModel;
    }

    /**
     * @param dbModel 原始执行sq结果
     * @return 达梦结果映射成List<Map < String, Object>>类型
     * @throws SQLException
     */
    private static List<Map<String, Object>> getMapping(DbModel dbModel) throws SQLException {
        List<Map<String, Object>> list;
        try {
            list = new ArrayList<>();
            ResultSetMetaData metaData = dbModel.getResultSet().getMetaData();
            int columnCount = metaData.getColumnCount();
            String[] columnName = new String[columnCount];
            for (int i = 1; i <= columnCount; i++) {
                columnName[i - 1] = metaData.getColumnName(i);
            }
            while (dbModel.getResultSet().next()) {
                Map<String, Object> map = new HashMap<>();
                for (int i = 0; i < columnName.length; i++) {
                    map.put(columnName[i], dbModel.getResultSet().getObject(columnName[i]));
                }
                list.add(map);
            }
        } finally {
            JdbcUtil.CloseConnection(null, dbModel.getStatement(), dbModel.getResultSet());
        }
        return list;
    }

    /**
     * @param dbModel
     * @return Mysql结果映射成List<Map < String, Object>>类型
     */
    private static List<Map<String, Object>> getMysqlMapping(DbModel dbModel) {
        return null;
    }

    /**
     * @param dbModel
     * @return Oracle结果映射成List<Map < String, Object>>类型
     */
    private static List<Map<String, Object>> getOracleMapping(DbModel dbModel) {
        return null;
    }

    public enum DbType {
        DM,//达梦数据库
        MYSQL,//mysql数据据
        ORACLE//oracle数据库
    }
}
